package studentsystem;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class query {
    private String num, nam;
    int i = 0;

    query() {
        Scanner s1 = new Scanner(System.in);
        int a = 3;
        while (a != 0) {
            System.out.println("---------------------");//查找界面并选择
            System.out.println("0 返回");
            System.out.println("1 查询成绩");
            System.out.println("请输入0或1：");
            a = s1.nextInt();
            switch (a) {
                case 0:
                    System.out.println("返回成功！");
                    break;
                case 1:
                    //数据库
                    SQL bd = new SQL();
                    Connection conn = bd.getConn();
                    try {
                        System.out.println("请输入姓名：");
                        nam = s1.next();
                        Statement stmt = conn.createStatement(); //创建Statement对象
                        String sql = "select * from students;";    //要执行的SQL
                        ResultSet rs = stmt.executeQuery(sql);//创建数据对象，产生单个结果集的语句
                        while (rs.next()) {
                            if (rs.getString("name").equals(nam)) {
                                i = 1;
                                System.out.println("成绩如下:");
                                //从数据库查找成绩
                                System.out.println();
                                System.out.println("姓名：" + rs.getString(1));
                                System.out.println("学号：" + rs.getString(2));
                                System.out.println("应用数学：" + rs.getDouble(3));
                                System.out.println("大学英语 ：" + rs.getDouble(4));
                                System.out.println("java程序设计：" + rs.getDouble(5));
                                System.out.println("计算机应用基础：" + rs.getDouble(6));
                                System.out.println("总分：" + rs.getDouble(7));
                                System.out.println("平均分：" + rs.getDouble(8));
                                System.out.println();
                                //数据库
                                break;
                            }

                        }
                        if (i == 0)
                            System.out.println("暂无该学生成绩！");
                        rs.close();
                        stmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }

    public static void main(String[] args) {
        new query();
    }

}